package yunwo.cn.yzstation.base;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;



import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 根类Activity
 * 
 */
public abstract class BaseActivity extends FragmentActivity {
	/** 记录处于前台的Activity */
	private static BaseActivity mForegroundActivity = null;
	/** 记录所有活动的Activity */
	private static final List<BaseActivity> mActivities = new LinkedList<BaseActivity>();
	private static final String TAG = "BaseActivity";
	public static final String SHAREPREFERENCE_NAME = "cwlptShareperference";
	private View headview;
	protected TextView btn_right;
	private RelativeLayout btn_right_rl;
//	public static ACache mCache;
	public static SharedPreferences sharedPreferences;
	public static SharedPreferences infoPreferences;
//	public static ACache sendGoodsCache;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
//		setBaiduInfo();
		super.onCreate(savedInstanceState);
//		mCache = ACache.get(this);
//		sendGoodsCache = ACache.get(this);
		mActivities.add(this);
		sharedPreferences = getSharedPreferences(SHAREPREFERENCE_NAME,
				Context.MODE_PRIVATE | Context.MODE_MULTI_PROCESS);
	}

	@Override
	public void setContentView(int layoutResID) {
		super.setContentView(layoutResID);
//		headview = this.findViewById(R.id.title_bar);
	}

	/**
	 *
	 * @param leftImg
	 *            0表示图标为 R.drawable.header_icon_back
	 * @param leftClick
	 * @param title
	 * @param rightClick
	 * @return
	 */
/*
	protected BaseActivity setTitleBar(int leftImg,
									   View.OnClickListener leftClick, String title, String rightStr,
									   View.OnClickListener rightClick) {
		if (leftImg == 0) {
			this.setLeftButton(R.drawable.header_return, leftClick)
					.setTitleBar(title).setRightButton(rightStr, rightClick)
					.showTitleBar();
		} else {
			this.setLeftButton(leftImg, leftClick).setTitleBar(title)
					.setRightButton(rightStr, rightClick).showTitleBar();
		}
		return this;
	}
*/

	/**
	 * 设置标题
	 * 
	 * @param title
	 * @return
	 */
	/*protected BaseActivity setTitleBar(String title) {
		TextView tv_title = (TextView) headview.findViewById(R.id.title);
		tv_title.setText(Html.fromHtml(title));
		tv_title.setVisibility(View.VISIBLE);
		return this;
	}
*/
	/**
	 * 显示头部左边的图标
	 * 
	 * @param imgRid
	 * @param onClick
	 * @return
	 */
/*	protected BaseActivity setLeftButton(int imgRid,
			View.OnClickListener onClick) {
		ImageView leftLL = (ImageView) headview.findViewById(R.id.leftLL);
		leftLL.setOnClickListener(onClick);
		leftLL.setVisibility(View.VISIBLE);
		leftLL.setImageResource(imgRid);
		return this;
	}*/

	/**
	 * 显示头部右边的图标
	 * 
	 * @param onClick
	 * @return
	 */
/*
	protected BaseActivity setRightButton(String rightStr,
			View.OnClickListener onClick) {
		btn_right = (TextView) headview.findViewById(R.id.rightTV);
		btn_right.setOnClickListener(onClick);
		btn_right.setText(rightStr);
		if (!StringUtil.isEmpty(rightStr)) {
			btn_right.setVisibility(View.VISIBLE);
			btn_right.setOnClickListener(onClick);
		}
		return this;
	}
*/

	/**
	 * 显示头部
	 * 
	 * @return
	 */
	protected BaseActivity showTitleBar() {
		headview.setVisibility(View.VISIBLE);
		return this;
	}

	/**
	 * 隐藏头部
	 * 
	 * @return
	 */
	protected BaseActivity hideTitleBar() {
		headview.setVisibility(View.GONE);
		return this;
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		 mActivities.remove(this);
	}

	@Override
	protected void onResume() {
		mForegroundActivity = this;
//		LogUtil.d(TAG, "mForegroundActivity:" + mForegroundActivity);
		super.onResume();
//		MobclickAgent.onResume(this);
//		MobclickAgent.onPageStart(getClass().toString());
	}

	@Override
	protected void onPause() {
		mForegroundActivity = null;
		super.onPause();
//		MobclickAgent.onPause(this);
//		MobclickAgent.onPageEnd(getClass().toString());
	}


	/** 是否有启动的Activity */
	public static boolean hasActivity() {
		return mActivities.size() > 0;
	}

	/** 获取当前处于前台的activity */
	public static BaseActivity getForegroundActivity() {
		return mForegroundActivity;
	}

	public static int getActivitySize() {
		return mActivities.size();
	}

	/** 获取当前处于栈顶的activity，无论其是否处于前台 */
	public static BaseActivity getCurrentActivity() {
		List<BaseActivity> copy;
		synchronized (mActivities) {
			copy = new ArrayList<BaseActivity>(mActivities);
		}
		if (copy.size() > 0) {
			return copy.get(copy.size() - 1);
		}
		return null;
	}

	/**
	 * 判断是否存在指定类的activity
	 * 
	 * @param clazz
	 * @return
	 */
	public static boolean hasActivity(Class clazz) {
		List<BaseActivity> copy;
		synchronized (mActivities) {
			copy = new ArrayList<BaseActivity>(mActivities);
		}
		for (BaseActivity activity : copy) {
			if (activity.getClass().getName().equals(clazz.getName())) {
				return true;
			}
		}
		return false;
	}
	/**
	 * 判断是否存在指定类的activity
	 *
	 * @param activity
	 * @return
	 */
	public static boolean hasActivity(Activity activity) {
		if(activity!=null){
			return mActivities.contains(activity);
		}
		return false;
	}

	/**
	 * 关闭指定类的activity
	 * 
	 * @param clazz
	 */
	public static void finishActivity(Class clazz) {
		List<BaseActivity> copy;
		synchronized (mActivities) {
			copy = new ArrayList<BaseActivity>(mActivities);
		}
		for (BaseActivity activity : copy) {
			if (activity != null
					&& activity.getClass().getName().equals(clazz.getName())) {
				activity.finish();
			}
		}
	}




}
